package com.shs.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shs.sys.common.DataGridView;
import com.shs.sys.common.ResultObj;
import com.shs.sys.common.TreeNode;
import com.shs.sys.entity.Dept;
import com.shs.sys.entity.Notice;
import com.shs.sys.service.DeptService;
import com.shs.sys.vo.DeptVo;
import com.shs.sys.vo.NoticeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 *  部门前端控制器
 * </p>
 *
 * @author jxm
 * @since 2020-04-10
 */
@RestController
@RequestMapping("dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 加载部门管理左边的部门树json
     * @param deptVo
     * @return
     */
    @RequestMapping("loadDeptManagerLeftTreeJson")
    public DataGridView loadDeptManagerLeftTreeJson(DeptVo deptVo){
        List<Dept> depts = deptService.list();
        List<TreeNode> treeNodes = new ArrayList<>();
        for (Dept dept : depts) {
            Boolean spread = dept.getOpen()==1?true:false;
            treeNodes.add(new TreeNode(dept.getId(),dept.getPid(),dept.getTitle(),spread));
        }
        return new DataGridView(treeNodes);
    }

    /**
     * 查询
     */
    @RequestMapping("loadAllDept")
    public DataGridView loadAllDept(DeptVo vo){
        Page<Dept> page = new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<Dept> wrapper = new QueryWrapper();
        wrapper.like(StringUtils.isNotBlank(vo.getTitle()),"title",vo.getTitle());
        wrapper.like(StringUtils.isNotBlank(vo.getAddress()),"address",vo.getAddress());
        wrapper.like(StringUtils.isNotBlank(vo.getRemark()),"remark",vo.getRemark());
        wrapper.eq(vo.getId()!=null,"id",vo.getId()).or()
                .eq(vo.getId()!=null,"pid",vo.getId());
        wrapper.orderByAsc("ordernum");
        deptService.page(page,wrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 获取部门排序码
     */
    @RequestMapping("loadDeptMaxOrderNum")
    public Map<String,Object> loadDeptMaxOrderNum(){
        Map<String,Object> result = new HashMap<>();
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("ordernum");
        List<Dept> depts = deptService.list(queryWrapper);
        if(null!=depts && depts.size()>0){
            result.put("value",depts.get(0).getOrdernum()+1);
        }else{
            result.put("value",1);
        }
        return result;
    }

    /**
     * 添加部门
     */
    @RequestMapping("addDept")
    public ResultObj addDept(DeptVo deptVo){
        try {
            deptVo.setCreatetime(new Date());
            deptService.save(deptVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改部门
     */
    @RequestMapping("updateDept")
    public ResultObj updateDept(DeptVo deptVo){
        try {
            deptService.updateById(deptVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     */
    @RequestMapping("deleteDept")
    public ResultObj deleteDept(DeptVo vo){
        try {
            deptService.removeById(vo.getId());
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 删除之前检查部门是否还有其他子部门
     */
    @RequestMapping("checkDeptHasChildrenNode")
    public Map<String,Object> checkDeptHasChildrenNode(Integer id){
        Map<String,Object> result = new HashMap<>();
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        wrapper.eq("pid",id);
        List<Dept> depts = deptService.list(wrapper);
        if(depts.size()>0){
            result.put("value",true);
        }else{
            result.put("value",false);
        }
        return  result;
    }

}
